package com.coderman.topn;

import com.alibaba.fastjson.JSON;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * @Author fanchunshuai
 * @Date 2019/11/22 23
 * @Description:
 * 模拟超过1亿随机数，从中找到出现次数最多的随机数
 */
public class TopNRandomNumber {
    public static void main(String[] args) throws IOException {

        /*try {
            //FileUtils.readLines(new File("D:\\tmp\\topn\\topnall\\allnumberfiles\\data_all_topnumbers.txt"),"UTF-8");
            LineIterator it = FileUtils.lineIterator(new File("D:\\tmp\\topn\\topnall\\allnumberfiles\\data_all_topnumbers.txt"), "UTF-8");
            try {
                while (it.hasNext()) {
                    String line = it.nextLine();
                }
            } finally {
                LineIterator.closeQuietly(it);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        FileInputStream inputStream = null;
        Scanner sc = null;
        try {
            String path = "D:\\tmp\\topn\\topnall\\allnumberfiles\\data_all_topnumbers.txt";
            inputStream = new FileInputStream(path);
            sc = new Scanner(inputStream, "UTF-8");
            while (sc.hasNextLine()) {
                String line = sc.nextLine();
            }
            if (sc.ioException() != null) {
                // throwsc.ioException();
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (sc != null) {
                sc.close();
            }
        }







        /*List<KVPair<String, Integer>> resultList = new ArrayList<>(10000);

        for (int i = 0; i < 100; i++) {
            String filePath = "E:\\tmp\\topn\\fileData\\" + "data_" + i + ".txt";
            Map<String, Integer> map = new HashMap<>(100000);
            try {
                List<String> fileLines = FileUtils.readLines(new File(filePath), "UTF-8");
                for (String str : fileLines) {
                    String[] array = str.replace("\n", "").split(",");
                    for (String number : array) {
                        Integer num = map.get(number);
                        if (num == null) {
                            map.put(number, 1);
                        } else {
                            map.put(number, num + 1);
                        }
                    }
                }
                System.out.println("i = " + i + ",map.size = " + map.size());
                List<KVPair<String, Integer>> kvPairList = map2KVPair(map);
                List<KVPair<String, Integer>> newKvPairList = kvPairList.stream()
                        .sorted(Comparator
                                .comparing((KVPair<String, Integer> p) -> p.getV())).collect(Collectors.toList());
                if (newKvPairList.size() > 100) {
                    resultList.addAll(newKvPairList.subList(newKvPairList.size() - 100, newKvPairList.size()));
                } else {
                    resultList.addAll(newKvPairList);
                }


            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        List<KVPair<String, Integer>> resultNewKvPairList = resultList.stream()
                .sorted(Comparator
                        .comparing((KVPair<String, Integer> p) -> p.getV())).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(resultNewKvPairList.subList(resultNewKvPairList.size() - 100, resultNewKvPairList.size())));*/

    }


    private static List<KVPair<String, Integer>> map2KVPair(Map<String, Integer> map) {
        List<KVPair<String, Integer>> kvPairList = new ArrayList<>(map.size());
        map.forEach((k, v) -> {
            kvPairList.add(KVPair.build(k, v));
        });
        return kvPairList;
    }


}
